<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="parmas.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="search" />
        <!-- 树形组件 -->
        <el-tree
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          node-key="id"
          :current-node-key="choseId"
          @node-click="nodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-type="'add-employee'" size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button v-type="'import-employee'" size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column align="center" label="头像" prop="staffPhoto">
            <template v-slot="{ row }">
              <!-- 头像 -->
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <!-- 用户姓  ?.可选链符 vue高版本可用-->
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="openRole(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            :page-size="parmas.pagesize"
            :current-page="parmas.page"
            layout="total,prev, pager, next"
            :total="total"
            @current-change="changePage"
          />
        </el-row>

        <import-excel :show-excel-dialog.sync="showExcelDialog" />

        <el-dialog :visible.sync="isShow" title="分配角色" width="40%">
          <div>
            <el-checkbox-group
              v-model="roleIds"
            >
              <el-checkbox v-for="item in allRole" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
          <div slot="footer" class="mod">
            <el-button @click="isShow=false">取 消</el-button>
            <el-button type="primary" @click="assignRoles">确 定</el-button>
          </div>

        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
// 引入表单组件
import ImportExcel from '@/views/employee/import-excel.vue'
// 引用获取部门api
import { getDepartment } from '@/api/department.js'
// 引用处理数据函数
import { ProcessingData } from '@/utils/index'
// 引用获取员工列表api
import { getEmployeeList, delEmployee, getQiRoleList, getOneEmployee, assignRoles } from '@/api/employee'
// 引入导出excel所需文件
import FileSaver from 'file-saver'
import { exportEmployee } from '@/api/employee'
export default {
  name: 'Employee',
  components: {
    // 注册组件
    ImportExcel
  },
  data() {
    return {
      // 树形组件配置
      allRole: [],
      roleIds: [],
      isShow: false,
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showExcelDialog: false, // 控制excel的弹层显示和隐藏
      depts: [], // 组织数据
      choseId: 1, // 默认选中的左侧传智教育
      // 发送请求时的数据
      parmas: {
        page: 1,
        pagesize: 10,
        keyword: '', // 模糊查询
        departmentId: 1// 部门id
      },
      // 获取到的数据渲染
      list: [],
      // 总页数
      total: 0
    }
  },
  // watch: {
  //   // 监听控制弹出框的数据
  //   isShow(newval) {
  //     // console.log(newval)
  //     if (newval === false) {

  //     }
  //   }
  // },
  created() {
    this.getDepartment()
    this.getEmployeeList()
    // console.log(this)
  },
  methods: {
    // 获取已启用角色
    async getQiRoleList(id) {
      const result = await getQiRoleList()
      // console.log(result)
      this.allRole = result.data
      // console.log(id)
      const res = await getOneEmployee(id)
      // console.log(res.data.roleIds)
      this.roleIds = res.data.roleIds
    },
    // 渲染角色列表
    async openRole(id) {
      await this.getQiRoleList(id)
      this.id = id
      this.isShow = true
    },
    // 修改角色权限
    async assignRoles() {
      await assignRoles(this.id, this.roleIds)
      this.$message.success('修改成功')
      this.isShow = false
    },
    // 获取部门
    async getDepartment() {
      const result = await getDepartment()
      // console.log(result)
      this.depts = ProcessingData(result.data, 0)
    },
    // 获取打工仔列表
    async  getEmployeeList() {
      const result = await getEmployeeList(this.parmas)
      // console.log(result)
      this.list = result.data.rows
      this.total = result.data.total
    },
    // 点击树形列表更新员工数据
    nodeClick(data) {
      // console.log(data)
      this.parmas.departmentId = data.id
      this.parmas.page = 1
      this.getEmployeeList()
    },
    // 切换分页时 请求新的数据
    changePage(newPage) {
      this.parmas.page = newPage // 赋值当前页码
      this.getEmployeeList()
    },
    // 模糊查找
    search() {
      clearTimeout(this.searchTime)
      this.searchTime = setTimeout(() => {
        this.getEmployeeList()
      }, 300)
    },
    // 导出excel 下载
    async  exportEmployee() {
      const result = await exportEmployee() // 导出所有的员工接口
      // console.log(result) // 使用一个npm包 直接将blob文件下载到本地 file-saver
      // FileSaver.saveAs(blob对象,文件名称)
      FileSaver.saveAs(result, '员工信息表.xlsx') // 下载文件
    },
    // 删除
    async confirmDel(id) {
      if (this.list.length === 1 && this.parmas.page > 1) {
        // 当前页面 最后一条数据  =>
        this.parmas.page--
      }
      // 删除数据
      await delEmployee(id)
      // 提示
      this.$message.success('删除员工成功')
      // 刷新数据
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.mod{
display: flex;
justify-content: center;
}
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
